package com.hmjk.health.user;

import com.hmjk.health.R;
import com.hmjk.health.util.FA_DATA;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BuildFamilyDialog extends Dialog implements TextWatcher, android.view.View.OnClickListener{
	private Context mContext;
	private static BuildFamilyDialog mSelf;
	private EditText mNameEdit;
	private EditText mStatusEdit;
	private CheckBox mAdminCheck;
	private LinearLayout bok;
	private LinearLayout bcancel;
	private static FA_DATA mData;
	private static boolean mToBeCreate = false;
	
	public interface OnFaDialogDismissListener{
		public void onDismiss(boolean toBeCreate, FA_DATA data);
	};
	
	protected BuildFamilyDialog(Context context, int theme) {
		super(context,theme);
		mContext = context;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		LayoutInflater inflater = LayoutInflater.from(mContext);
		View v = inflater.inflate(R.layout.build_family, null);
		setContentView(v);
		
		Window win = getWindow();
		win.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		win.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		
		mNameEdit = (EditText) v.findViewById(R.id.fa_name);
		mNameEdit.addTextChangedListener(this);
		mStatusEdit = (EditText) v.findViewById(R.id.fa_status);
		mStatusEdit.addTextChangedListener(this);
		
		mAdminCheck = (CheckBox) v.findViewById(R.id.me_admin);
		bok = (LinearLayout) v.findViewById(R.id.but_ok);
		setGroupEnabled(bok, false);
		bcancel = (LinearLayout) v.findViewById(R.id.but_cancel);
		setGroupEnabled(bcancel, true);
		bok.setOnClickListener(this);
		bcancel.setOnClickListener(this);
		mToBeCreate = false;
		mData = new FA_DATA();
	}
	
	private void setGroupEnabled(LinearLayout group, boolean enabled){
		group.setEnabled(enabled);
		ImageView img = (ImageView) group.getChildAt(0);
		img.setEnabled(enabled);
		TextView txt = (TextView) group.getChildAt(1);
		txt.setEnabled(enabled);
		txt.setTextColor(enabled?0XFFFFFFFF:0XFF7B7B7B);
	}

	public static void open(Context context, final OnFaDialogDismissListener l){
		if(mSelf == null){
			mSelf = new BuildFamilyDialog(context, R.style.CustomDialog);
		}
		mSelf.setCanceledOnTouchOutside(false);
		mSelf.setCancelable(true);
		mSelf.setOnDismissListener(new OnDismissListener(){

			@Override
			public void onDismiss(DialogInterface arg0) {
				if(l != null){
					l.onDismiss(mToBeCreate, mData);
				}
				mData = null;
				mToBeCreate = false;
				mSelf = null;
			}
			
		});
		mSelf.show();
	}

	@Override
	public void afterTextChanged(Editable e) {
		String name = mNameEdit.getText().toString().trim();
		String status = mStatusEdit.getText().toString().trim();
		if((name.length() > 0) && (status.length() > 0)){
			setGroupEnabled(bok,true);
		}else{
			setGroupEnabled(bok,false);
		}
		
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.but_ok){
			mData.name = mNameEdit.getText().toString().trim();
			mData.status = mStatusEdit.getText().toString().trim();
			mData.admin = mAdminCheck.isChecked();
			mToBeCreate = true;
			mSelf.dismiss();
			
		}else if(v.getId() == R.id.but_cancel){
			mToBeCreate = false;
			mSelf.dismiss();
		}
	}
	
}
